<template>
  <div>
    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">楼层列表</div>
        <div class="op_right">
          <button class="primary_button" @click="NewSort">添加楼层</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="floorData" stripe style="width: 100%">
          <el-table-column prop="sort" label="排序号" width="300px">
          </el-table-column>
          <el-table-column prop="value" label="楼层数"> </el-table-column>

          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="EditSort(scope.row)"
                >编辑</el-button
              >
              <span class="separate_right">|</span>
              <el-button size="mini" type="text" @click="DelSort(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 添加楼层 -->
      <el-dialog
        :visible.sync="SetdialogVisible"
        width="400px"
        :title="title"
        class="no-dialog-padding"
        :before-close="SethandleClose"
      >
        <span>
          <div class="source_box">
            <el-form
              ref="floorSortForm"
              :rules="floorSortRules"
              :model="floorSortForm"
            >
              <el-row>
                <el-form-item label="排序号" prop="sort">
                  <el-input-number
                    class="w-100 text-left"
                    placeholder="请输入排序号"
                    v-model="floorSortForm.sort"
                    :min="0"
                    :step="1"
                    :precision="0"
                    :controls="false"
                  ></el-input-number>
                </el-form-item>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label="楼层数" prop="value">
                    <el-input
                      class="w-100"
                      v-model="floorSortForm.value"
                      placeholder="请输入楼层数"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="SethandleClose"
            >取 消</el-button
          >
          <el-button type="primary" @click="FloorSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
        
        <script>
import {
  getRoomList,
  CreateRoomSet,
  UpdateRoomSet,
  DeleteRoom,
} from "@/api/business/roomSet";
export default {
  name: "EryuoaStoreRoomSetFloor",

  data() {
    return {
      SetdialogVisible: false, // 分类

      activeIndex: 1,
      form: {
        name: "",
      },

      title: "",
      value1: "",
      floorData: [],

      floorSortRules: {
        value: [{ required: true, message: "请输入楼层数", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序号", trigger: "blur" }],
      },

      floorSortForm: {
        value: "",
        sort: "",
      },

      value: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        type: 1, // 设置类型：1楼层设置2房间类型3床位管理
      },
      total: 0,
    };
  },

  mounted() {},

  methods: {
    resQ() {
      this.queryParams.pageNo = 1;
      this.total = 0;
      this.floorData = [];
      this.getList();
    },

    // 楼层列表
    getList() {
      this.loading = true;
      getRoomList(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.floorData = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 新建来源
    NewSort() {
      this.SetdialogVisible = true;
      this.title = "添加楼层";
    },

    // 确认
    FloorSubmit() {
      this.$refs["floorSortForm"].validate((valid) => {
        if (valid) {
          if (!!this.floorSortForm.id) {
            this.EditFloor();
          } else {
            this.NewFloor();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    SethandleClose() {
      this.resetFormData("floorSortForm");
      this.$nextTick(() => {
        this.SetdialogVisible = false;
      });
    },

    // 表单重置
    resetFormData(formName) {
      // console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 新建
    NewFloor() {
      let floorSortForm = JSON.parse(JSON.stringify(this.floorSortForm));
      floorSortForm.type = 1;
      CreateRoomSet(floorSortForm)
        .then((Res) => {
          const { code } = Res;
          if (code === 0) {
            this.getList();
            this.SethandleClose();
          }
        })
        .catch((err) => {});
    },

    // 编辑来源
    EditSort(row) {
      this.SetdialogVisible = true;
      this.title = "编辑楼层";
      this.$nextTick(() => {
        const item = JSON.parse(JSON.stringify(row));
        this.floorSortForm.sort = item.sort;
        this.floorSortForm.value = item.value;
        this.floorSortForm.type = item.type;
        this.floorSortForm.id = item.id;
      });
    },

    // 编辑弹框 确认
    EditFloor() {
      UpdateRoomSet(this.floorSortForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.SethandleClose();
          }
        })
        .catch((err) => {});
    },

    // 删除来源
    DelSort(row) {
      this.DeleteSort(row);
    },

    DeleteSort(row) {
      let id = row.id;
      this.$confirm("删除楼层后无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteRoom({ id: id }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {});
    },

    // 新增楼层 弹框 确认
    ClientSubmit() {
      this.$refs["newClientForm"].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
        
<style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
  margin-right: 12px;
}

.source_box {
  margin: 0 24px;
}
</style>